package a_aa_amainpackage.lvyou_clear.lv_update

import com.alibaba.fastjson.JSON
import com.mongodb.spark.MongoSpark
import com.mongodb.spark.config.ReadConfig
import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}

/*
*项目名: o2odata_commend
*文件名: frommongo
*创建者: hancongcong
*创建时间:2020/3/3 14:53
*描述: TODO
*/ object frommongo {

  val mongopeizhi = "mongodb://root:O2Odata123!@ 114.115.144.192:27017/admin"
  val data_base = "Tuniu"
  val date = "2208"
  val year = "2022"
  val month = "8"


  def main(args: Array[String]): Unit = {
    /**
      * Tuniu.tuniu_ly_detail_2022
      * Tuniu.
      * Tuniu.tuniu_yl_detail_2022tuniu_mp_detail_2022
      */
    val sc = get_sparkContext()
    val sqlContext = new SQLContext(sc)
    //途牛旅游
    mongo_totalsell(sqlContext, sc, s"${data_base}", s"tuniu_ly_detail_${date}", s"s3a://o2o-tempdata/zsc/${year}/${month}/${data_base}/tuniu_ly")
    //    mongo_totalsell(sqlContext,sc,s"${data_base}",s"tuniu_ly_detail_${date}",s"s3a://o2o-dataproces-group/zsc/${year}/${month}/${data_base}/tuniu_ly")
    //途牛游轮
    mongo_totalsell(sqlContext, sc, s"${data_base}", s"tuniu_mp_detail_${date}", s"s3a://o2o-tempdata/zsc/${year}/${month}/${data_base}/tuniu_mp")
    //    mongo_totalsell(sqlContext,sc,s"${data_base}",s"tuniu_mp_detail_${date}",s"s3a://o2o-dataproces-group/zsc/${year}/${month}/${data_base}/tuniu_mp")
    //途牛门票
    mongo_totalsell(sqlContext, sc, s"${data_base}", s"tuniu_yl_detail_${date}", s"s3a://o2o-tempdata/zsc/${year}/${month}/${data_base}/tuniu_yl")
    //    mongo_totalsell(sqlContext,sc,s"${data_base}",s"tuniu_yl_detail_${date}",s"s3a://o2o-dataproces-group/zsc/${year}/${month}/${data_base}/tuniu_yl")
    //门票地址


  }

  def mongo_totalsell(sqlContext: SQLContext, sc: SparkContext, database: String, collection: String, sourcepath: String): Unit = {
    val brandapi_vf = ReadConfig(Map(
      "spark.mongodb.input.uri" -> s"${mongopeizhi}"
      , "spark.mongodb.input.database" -> s"${database}"
      , "spark.mongodb.input.collection" -> s"${collection}"))
    val brandapi_vf_df = sqlContext.read.json(MongoSpark.load(sc, brandapi_vf).map(line => {
      val nObject = JSON.parseObject(line.toJson())
      nObject.remove("_id")
      nObject.toJSONString
    })).repartition(5).write.mode("overwrite")
      .json(s"${sourcepath}")
  }

  //拉数
  def get_sparkConf(): SparkConf = {
    val conf = new SparkConf()
    conf.setAppName(s"${this.getClass.getSimpleName}")
    conf.set("spark.debug.maxToStringFields", "500")
    conf.setMaster("local[*]")
    conf.set("es.nodes", "192.168.1.29")
    conf.set("es.port", "9200")
    conf.set("cluster.name", "O2OElastic")
    conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    conf.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    conf.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    conf.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")
    conf
  }


  def get_sparkContext(): SparkContext = {
    val conf = get_sparkConf()
    val sc = new SparkContext(conf)
    sc.setLogLevel("WARN")
    sc
  }


}
